package com.tcp2.util;

import com.google.common.collect.Maps;
import org.apache.commons.configuration.PropertiesConfiguration;

import javax.swing.*;
import java.util.*;
import java.util.concurrent.ConcurrentMap;

/**
 * Author: 张锦煌
 * Time: 2015-08-20 08:28
 * Copyright (C) 2015 Xiamen Yaxon Networks CO.,LTD.
 * 存储中间数据类
 */

public   class  MyQuene {

    private static  LinkedList<List> msgList = new LinkedList<>();     //操作日志队列
    private static  List<Long> simList = new ArrayList<>();          //存储被注销车辆
    public static  ConcurrentMap<Long,Integer> taskMap = Maps.newConcurrentMap();      //存储中心下发订单
    private static ConcurrentMap<Long, String> authcodeMap = Maps.newConcurrentMap();    //存储全部已注册车辆鉴权码
    private static ConcurrentMap<Long, String> newAuthcodeMap = Maps.newConcurrentMap();  //存储当前注册车辆的鉴权码

    public static boolean gpsState;   //定时发送GPS线程标志
    public static boolean photoState;   //定时发送图片线程标志
    private static  List<JTable> tableStu = new ArrayList<>(1);     //表格缓存




    public static  List getMsg() {
        synchronized(MyQuene.class) {
            if (msgList != null && msgList.size() > 0) {
                return msgList.removeFirst();
            }
        }
            return null;
    }

    public static  void addMsg(List obj) {
        synchronized(MyQuene.class) {
            msgList.addLast(obj);
        }
    }

    public static  Integer removeTask(Long sim) {
        synchronized(MyQuene.class) {
            if (taskMap.isEmpty()) {
                return 0;
            } else {
                return taskMap.remove(sim);
            }
        }
    }

    public static  void addTask(Long sim,Integer callId) {
        synchronized(MyQuene.class) {
            if(taskMap.putIfAbsent(sim,callId)!=null){
                new Exception("生成重复的存根数据：" + sim);
            }
        }
    }

    public static  void addSim(Long sim) {
        synchronized(MyQuene.class) {
            simList.add(sim);
            authcodeMap.remove(sim);
        }
    }

    public static void delSim() throws Exception{
        synchronized(MyQuene.class) {
            if (simList != null && simList.size() > 0) {
                PropertiesConfiguration config = null;
                config = new PropertiesConfiguration("./conf/authcode.properties");
                config.setAutoSave(true);
                 for(long sim:simList){
                     config.clearProperty(String.valueOf(sim));
                 }
            }
        }
    }

    public static void putAuthcode(Long sim,String authcode) {
        synchronized (MyQuene.class){
        if (authcodeMap.putIfAbsent(sim, authcode) != null) {
            new Exception("生成重复的存根数据：" + sim);
        }
    }
    }

    public static void putAuthcode1(Long sim,String authcode) {
        synchronized (MyQuene.class){
            if (newAuthcodeMap.putIfAbsent(sim, authcode) != null) {
                new Exception("生成重复的存根数据：" + sim);
            }
            simList.remove(sim);
        }
    }

    public static String getAuthcode(Long sim) {
        synchronized (MyQuene.class) {
            if (authcodeMap.isEmpty()) return null;
            else
                return authcodeMap.get(sim);
        }
    }

    public  static void itMap() throws Exception {
        synchronized (MyQuene.class) {
            if (newAuthcodeMap.isEmpty()) return;
            else {
                Iterator it = newAuthcodeMap.entrySet().iterator();
                String key;
                String value;
                PropertiesConfiguration config = new PropertiesConfiguration("./conf/authcode.properties");
                config.setAutoSave(true);
                while (it.hasNext()) {
                    Map.Entry entry = (Map.Entry) it.next();
                    key = entry.getKey().toString();
                    value = entry.getValue().toString();
                    config.setProperty(key, value);
                }
                newAuthcodeMap.clear();
                config.clone();
            }
        }
    }

    public static void addTable(JTable table){
        tableStu.add(table);
    }

    public static JTable getTable(){
        return tableStu.get(0);
    }
}
